package com.example.lin.day9_3;


import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    private RecyclerView rv_items;
    private ArrayList<RVItem> rvItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();


    }

    private void initData() {
        rvItems = new ArrayList<>();
        rvItems.add(new RVItem(R.mipmap.img1, "天外飞仙"));
        rvItems.add(new RVItem(R.mipmap.img2, "天外飞仙2"));
        rvItems.add(new RVItem(R.mipmap.img3, "天外飞仙3"));
        rvItems.add(new RVItem(R.mipmap.img4, "天外飞仙5"));
        rvItems.add(new RVItem(R.mipmap.img5, "天外飞仙4"));
        rvItems.add(new RVItem(R.mipmap.img6, "天外飞仙6"));
        rvItems.add(new RVItem(R.mipmap.img7, "天外飞仙7"));
        rvItems.add(new RVItem(R.mipmap.img8, "天外飞仙8"));
        rvItems.add(new RVItem(R.mipmap.img9, "天外飞仙112"));
        rvItems.add(new RVItem(R.mipmap.img10, "天外飞9仙"));
        rvItems.add(new RVItem(R.mipmap.img11, "天外飞仙32"));
        rvItems.add(new RVItem(R.mipmap.img12, "天外飞仙3243"));
        rvItems.add(new RVItem(R.mipmap.img13, "天外飞仙45"));
        rvItems.add(new RVItem(R.mipmap.img14, "天外飞仙77"));
    }

    private void initView() {
        rv_items = findViewById(R.id.rv_items);

        //[必须设置] 设置 RecyclerView 的布局管理器.(告诉RecyclerView 的布局模式:线性(水平/垂直),网格(水平/垂直))
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, RecyclerView.HORIZONTAL, false);
        rv_items.setLayoutManager(gridLayoutManager);

        //[可选]添加默认分隔线
//        rv_items.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));


        MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter(this);
        rv_items.setAdapter(myRecyclerAdapter);

        myRecyclerAdapter.setList(rvItems);
    }

}
